var util = require('../../libs/util.js');
var app = getApp();
var sliderWidth = 120; // 需要设置slider的宽度，用于计算中间位置
var startX = 0, endX = 0, prevScrollLeft = 0, prevId = 0, prevIndex = 0;
var px = 0, py = 0, px2 = 0, py2 = 0, windowWidth = 0;
Page({
  data: {
    hasData: false,
    middle_height: "100vh",
    delivery_date: util.dateObjectformat(new Date(), 'yyyy-MM-dd'),
    delivery_date_text: "今天配送货物",
    shop_data: [],
    detailShow: '',
    dd_style: "",
    path_data: []
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onReady: function (e) {
  },
  onLoad: function () {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--平台调度端" });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - 92,
          detail_height: res.windowHeight - 40
        });
        windowWidth = res.windowWidth;
      }
    });
    this.loadData();
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  loadData: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app.server + '/wechat/wxapplet/getPackageEveryday',
      method: 'GET',
      header: {
        'content-type': 'application/json',
        'Cookie':app.globalData.cookie
      },
      data: {
        delivery_date: that.data.delivery_date
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        var shop_data = res.data.data[0], package_data = res.data.data[1], _package_data = [];
        if (res.data.success){
          if (package_data.length > 0) {
            for (var i = 0; i < package_data.length; i++) {
              _package_data[package_data[i].shop] = package_data[i].packages;
            }
          } console.log(_package_data)
          if (shop_data.length > 0) {
            for (var i = 0; i < shop_data.length; i++) {
              shop_data[i].packages = _package_data[shop_data[i].ID]||0;
            }
            that.setData({
              hasData: true,
              shop_data: shop_data
            });
          } else {
            that.setData({
              hasData: false,
              shop_data: []
            })
          };
        } else {
          that.setData({
            hasData: false,
            shop_data: []
          })
        };
        
      },
      fail: function (error) {
        wx.hideLoading();
        console.log(error);
        that.setData({
          hasData: false,
          shop_data: []
        })
      }
    })
  },
  updateShopPackages:function(e){
    var that = this;
    var shop = e.currentTarget.dataset.shop;
    var value = e.detail.value;
    util.postData("updateShopPackages", { 'shop': shop, 'packages': value, 'delivery_date': that.data.delivery_date}, function (res, error) {
      console.log(res); console.log(error)
      if (error) {
        wx.showToast({
          title: "更新失败",
          image: '/images/wrong.png',
          duration: 3000
        });
        return false;
      }
      if (res.data.success) {
        wx.showToast({
          title: "已更新",
          icon: "success",
          duration: 2000
        });
      } else if (res.data.success == false) {
        wx.showToast({
          title: "更新失败",
          image: '/images/wrong.png',
          duration: 3000,
          mask: true
        });
      }
    });
  },
  changeDeliveryDate: function (e) {
    this.setData({
      delivery_date: e.detail.value,
      delivery_date_text: e.detail.value
    });
    this.loadData();
  },
  showDeliveryDetail: function (e) {
    var that = this;
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/order/order?id=' + id
    })
  },
  startSpeechRecognition: function (e) {
    var that = this;
    util.speechRecognition(function (data) {
      if (data) {
        var data = String(data).split("，")[0];
        that.AI(data);
      } else {
        wx.showToast({
          title: '识别失败',
          image: '/images/wrong.svg',
          duration: 2000,
          mask: true
        })
      }
    });
  },
  stopSpeechRecognition: function (e) {
    wx.stopRecord();
    wx.hideToast();
  },
  AI: function (text) {
    var that = this;
    wx.showToast({
      title: "搜索：" + text,
      icon: 'success',
      duration: 20000,
      mask: true
    });
    var delivery_data = that.data.delivery_data, shop_data = that.data.shop_data, isFind = false;
    for (var i = 0; i < delivery_data.length; i++) {
      if (String(delivery_data[i].path_name).indexOf(text) > -1
        || String(delivery_data[i].driver_name).indexOf(text) > -1
        || String(delivery_data[i].driver_phone).indexOf(text) > -1
        || String(delivery_data[i].plate).indexOf(text) > -1) {
        that.setMap(delivery_data[i].ID);
        isFind = true;
        wx.hideToast();
        break;
      }
    };
    if (!isFind) {
      for (var i = 0; i < shop_data.length; i++) {
        if (String(shop_data[i].name).indexOf(text) > -1) {
          that.setMap(shop_data[i].ID);
          isFind = true;
          wx.hideToast();
          break;
        }
      };
    }
    if (!isFind) {
      wx.showToast({
        title: "没有数据",
        image: '/images/wrong.svg',
        duration: 2000,
        mask: true
      });
    }
  },
  gotoHome: function () {
    wx.redirectTo({
      url: '/pages/schedule/schedule'
    })
  },
  gotoPathNew: function () {
    wx.redirectTo({
      url: '/pages/index/deliveryForm'
    })
  },
  gotoMonitor: function () {
    wx.redirectTo({
      url: '/pages/schedule/monitor'
    })
  },
  gotoPath: function () {
    wx.redirectTo({
      url: '/pages/schedule/path'
    })
  },
  gotoPackage: function () {
    wx.redirectTo({
      url: '/pages/schedule/package'
    })
  },
  gotoWarehouse: function () {
    wx.redirectTo({
      url: '/pages/schedule/warehouse'
    })
  },
  gotoShop: function () {
    wx.redirectTo({
      url: '/pages/schedule/shop'
    })
  },
  gotoInfo: function () {
    wx.redirectTo({
      url: '/pages/schedule/apps'
    })
  }
})
